#ifndef _THREAD_H_
#define _THREAD_H_

#include <iostream>
#include <cstring>
#include <cstdio>
#include <functional>
#include <stdint.h>

namespace ThreadModle
{

    static uint32_t number = 1;
    class Thread
    {
        using func_t = std::function<void()>;

    private:
        void EnableRunning()
        {
            _isrunning = true;
        }

        void EnableDetach()
        {
            std::cout << "该线程分离" << std::endl;
            _isdetach = true;
        }

        static void *Routine(void *args)
        {
            Thread *self = static_cast<Thread *>(args);
            self->EnableRunning();
            if (self->_isdetach)
                self->Detach();
            pthread_setname_np(self->_tid, self->_name.c_str());
            self->_func();
            return nullptr;
        }

    public:
        Thread(func_t func)
            : _func(func), _tid(0), _isrunning(false), _isdetach(false), res(nullptr)
        {
            _name = "thread-" + std::to_string(number++);
        }

        void Detach()
        {
            if (_isrunning)
            {
                if (_isdetach)
                    return;
                pthread_detach(_tid);
                EnableDetach();
            }
            else
                std::cout << "线程未运行，无法分离，请先运行线程！！！" << std::endl;
        }

        bool Start()
        {
            int n = pthread_create(&_tid, nullptr, Routine, this);
            if (n != 0)
            {
                std::cerr << "thread create error: " << strerror(n) << std::endl;
                return false;
            }
            else
            {
                std::cout << "thread cerate success: " << _name << std::endl;
                return true;
            }
        }

        bool Stop()
        {
            if (_isrunning)
            {
                int n = pthread_cancel(_tid);
                if (n != 0)
                {
                    std::cerr << "thread cancel error: " << strerror(n) << std::endl;
                    return false;
                }
                else
                {
                    _isrunning = false;
                    std::cout << "cancel thread success: " << _name << std::endl;
                    return true;
                }
            }
            return false;
        }

        void Join()
        {
            if(_isdetach)
            {
                std::cout << "线程已经分离，无法进行join" << std::endl;
                return;
            }
            else
            {
               int n = pthread_join(_tid, &res);
               if(n != 0)
               {
                    std::cerr << " thread join error: " << strerror(n) << std::endl;
                    return;
               }
               else
               {
                    std::cout << "thread join success: " << _name <<std::endl;
               }
            }
        }

        ~Thread()
        {
        }

    private:
        pthread_t _tid;
        bool _isrunning;
        bool _isdetach;
        void *res;
        std::string _name;
        func_t _func;
    };

}

#endif